package com.example.training.controller;

import com.example.training.model.entity.Student;
import com.example.training.model.web.Result;
import com.example.training.model.web.Results;
import com.example.training.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private  StudentService studentService;

    //todo 1. Please modify the response structure so that they all return data in a response format,
    //    such as code, message, data. Where data can be a single object, a collection, or a basic data type.

    //todo 2. Please add the validation for API add and update, some  of the parameters are required.

    /**
     * @Author huaginlin
     * @Description 1. Group by 'sex' attribute into a Map      
     * @date 11:54 2025/5/15
     * @Param []
     * @Return com.example.training.model.web.Result<java.util.Map<java.lang.String,java.util.List<com.example.training.model.entity.Student>>>
     **/
    @GetMapping("/group-by-sex")
    public Result<Map<String, List<Student>>> groupStudentsBySex() {
        return Results.success(studentService.groupStudentsBySex());
    }

    /**
     * @Author huaginlin
     * @Description 2. Group students by their surname's first letter      
     * @date 11:54 2025/5/15
     * @Param []
     * @Return com.example.training.model.web.Result<java.util.Map<java.lang.String,java.util.List<com.example.training.model.entity.Student>>>
     **/
    @GetMapping("/group-by-first-letter")
    public Result<Map<String, List<Student>>> groupStudentsByFirstLetter() {
        return Results.success(studentService.groupStudentsByFirstLetter());
    }

    /**
     * @Author huaginlin
     * @Description 3. Find students with duplicate names      
     * @date 11:54 2025/5/15
     * @Param []
     * @Return com.example.training.model.web.Result<java.util.List<java.lang.String>>
     **/
    @GetMapping("/distinct-names")
    public Result<List<String>> getDistinctNames() {
        return Results.success(studentService.getDistinctNames());
    }

    /**
     * @Author huaginlin
     * @Description 4. Group students by name length (2, 3, or 4 characters)      
     * @date 11:54 2025/5/15
     * @Param []
     * @Return com.example.training.model.web.Result<java.util.Map<java.lang.Integer,java.util.List<com.example.training.model.entity.Student>>>
     **/
    @GetMapping("/group-by-name-length")
    public Result<Map<Integer, List<Student>>> groupStudentsByNameLength() {
        return Results.success(studentService.groupStudentsByNameLength());
    }

    /**
     * @Author huaginlin
     * @Description 5. Sort students by ID (ascending or descending order)
     * @date 11:52 2025/5/15
     * @Param [desc]
     * @Return com.example.training.model.web.Result<java.util.List<com.example.training.model.entity.Student>>
     **/
    @GetMapping("/sorted")
    public Result<List<Student>> getSortedStudents(@RequestParam(defaultValue = "0") int desc) {
        return Results.success(studentService.getSortedStudents(desc));
    }

    /**
     * @Author huaginlin
     * @Description 6. Get distinct hometowns concatenated as comma-separated string
     * @date 11:52 2025/5/15
     * @Param []
     * @Return com.example.training.model.web.Result<java.lang.String>
     **/
    @GetMapping("/distinct-hometowns")
    public Result<String> getDistinctHometowns() {
        return Results.success(studentService.getDistinctHometowns());
    }


    /**
     * @Author huaginlin
     * @Description 7. Add new student
     * @date 11:52 2025/5/15
     * @Param [student]
     * @Return com.example.training.model.web.Result<java.lang.Boolean>
     **/
    @PostMapping("/add")
    public Result<Boolean> addStudent(@RequestBody Student student) {
        return Results.success(studentService.addStudent(student));
    }

    /**
     * @Author huaginlin
     * @Description 8. update student by ID
     * @date 11:51 2025/5/15
     * @Param [id, student]
     * @Return com.example.training.model.web.Result<java.util.List<com.example.training.model.entity.Student>>
     **/
    @GetMapping("/update")
    public Result<List<Student>> updateStudents(@RequestParam int id,
                                                @RequestBody Student student) {
        return Results.success(studentService.updateStudents(id, student));
    }

    /**
     * @Author huaginlin
     * @Description 9. Delete student by ID
     * @date 11:50 2025/5/15
     * @Param [id]
     * @Return com.example.training.model.web.Result<java.util.List<com.example.training.model.entity.Student>>
     **/
    @GetMapping("/remove")
    public Result<List<Student>> removeStudentsById(@RequestParam int id) {
        return Results.success(studentService.removeStudentsById(id));
    }
}